home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 6.6 KB | 242 lines |
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- public class Controller extends Window
- {
- //Declare constants
- //Insert "Controller Constants"
- public static final String BACKWARD_COMMAND = "backward";
- public static final String FORWARD_COMMAND = "forward";
- public static final String PLAY_COMMAND = "play";
- public static final String PAUSE_COMMAND = "pause";
-
- protected static final String imagePath = "images/controlborder.jpg";
- protected static final int collapsedWidth = 9;
-
- //Declare data members
- //Insert "Controller data members"
- protected Image image;
- protected Frame frame;
-
- protected BackwardButton backwardButton1;
- protected PlayPauseButton playPauseButton1;
- protected ForwardButton forwardButton1;
- protected CloseBoxButton closeBoxButton1;
- protected DumbContainer container;
-
- protected ActionListener actionListener = null;
-
- /**
- * Creates a Controller object with the specified parent Frame.
- * @param the parent frame to associate the controller with.
- */
- public Controller(Frame parent)
- {
- //Setup parent info for this window
- //Insert "Controller parent info"
- super(parent);
- frame = parent;
-
- //INIT_CONTROLS
- //Setup and layout objects in the controller
- //Insert "Controller init controls"
- setVisible(false);
- setLayout(null);
- setSize(90,30);
-
- closeBoxButton1 = new CloseBoxButton();
- closeBoxButton1.setLocation(1,1);
- closeBoxButton1.setSize(closeBoxButton1.getPreferredSize());
- add(closeBoxButton1);
-
- container = new DumbContainer();
- container.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
- container.setBounds(9, 1, 81, 28);
-
- backwardButton1 = new BackwardButton();
- backwardButton1.setBounds(10,1,20,40);
- container.add(backwardButton1);
-
- playPauseButton1 = new PlayPauseButton();
- playPauseButton1.setBounds(0,0,20,40);
- container.add(playPauseButton1);
-
- forwardButton1 = new ForwardButton();
- forwardButton1.setBounds(0,0,20,40);
- container.add(forwardButton1);
-
- add(container);
-
- //REGISTER_LISTENERS
- //Register our action listener with our buttons
- //Insert "Controller register listeners"
- Action aAction = new Action();
- backwardButton1.addActionListener(aAction);
- forwardButton1.addActionListener(aAction);
- playPauseButton1.addActionListener(aAction);
- closeBoxButton1.addActionListener(aAction);
-
- //Initialize state information.
- //Insert "Controller init state"
- image = Misc.loadImage(imagePath, parent, true);
-
- setSize(getPreferredSize());
- //Work around a MRJ Bug.
- setLocation(-5,-21);
- }
-
- /**
- * Set the state of the Play/Pause button
- * @param if true, the button will be in the Play state;
- * If false it will be in the Pause state.
- * @see #isPlayState
- */
- public void setPlayState(boolean isPlay)
- {
- //Handle setup for the appropriate state
- //Insert "Controller setPlayState"
- if (isPlay)
- playPauseButton1.setState(PlayPauseButton.PLAY_STATE);
- else
- playPauseButton1.setState(PlayPauseButton.PAUSE_STATE);
- }
-
- /**
- * Get the current state of the Play/Pause button
- * @return true if the button is in the Play state,
- * false if it is in the Pause state.
- * @see #setPlayState
- */
- public boolean isPlayState()
- {
- //Return the current state
- //Insert "Controller isPlayState"
- return playPauseButton1.getState() == PlayPauseButton.PLAY_STATE;
- }
-
- //Routines for handling ActionListener management.
- //Insert "Controller Action Management"
- /**
- * Adds the specified action listener to receive action events.
- * @param l the action listener
- */
- public void addActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.add(actionListener, l);
- }
-
- /**
- * Removes the specified action listener so it no longer receives
- * action events from this component.
- * @param l the action listener
- */
- public void removeActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.remove(actionListener, l);
- }
-
- /**
- * Fire an action event to the listeners.
- * @param command, the command String to send with the ActionEvent
- */
- protected void fireActionEvent(String command)
- {
- if (actionListener != null)
- actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, command));
- }
-
- public void paint(Graphics g)
- {
- //Handle painting the border image, and let the super class paint the rest.
- //Insert "Controller paint"
- if (image != null)
- g.drawImage(image, 0, 0, this);
- super.paint(g);
- }
-
- public void update(Graphics g)
- {
- //Override update to simply call paint to reduce flicker.
- //Insert "Controller update"
- paint(g);
- }
-
- /**
- * Gets the size the controller should be to look its best
- * @return the dimensions the controller renders its self the best.
- */
- public Dimension getPreferredSize()
- {
- //If the current image is not null, then return the size of the image.
- //If it is null, defer to the super class.
- //Insert "Controller getPreferredSize"
- if (image != null)
- return new Dimension (image.getWidth(frame), image.getHeight(frame));
-
- return super.getPreferredSize();
- }
-
- //Inner class so we can instantiate a simple lightweight container
- //for use in laying out the controller properly.
- class DumbContainer extends Container { }
-
- //Inner class to handle action events
- //Insert "Controller Action"
- class Action implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- Object object = event.getSource();
- if (object == backwardButton1)
- backwardButton1_ActionPerformed(event);
- else if (object == forwardButton1)
- forwardButton1_ActionPerformed(event);
- else if (object == playPauseButton1)
- playPauseButton1_ActionPerformed(event);
- else if (object == closeBoxButton1)
- closeBoxButton1_ActionPerformed(event);
-
- }
- }
-
- //Routines to handle action events from the different buttons
- //Insert "Controller button actions"
- void backwardButton1_ActionPerformed(ActionEvent event)
- {
- fireActionEvent(BACKWARD_COMMAND);
- }
-
- void forwardButton1_ActionPerformed(ActionEvent event)
- {
- fireActionEvent(FORWARD_COMMAND);
- }
-
- void playPauseButton1_ActionPerformed(ActionEvent event)
- {
- String command = event.getActionCommand();
- try
- {
- int state = Integer.valueOf(command).intValue();
- switch (state)
- {
- case PlayPauseButton.PLAY_STATE:
- fireActionEvent(PLAY_COMMAND);
- playPauseButton1.setState(PlayPauseButton.PAUSE_STATE);
- break;
- case PlayPauseButton.PAUSE_STATE:
- fireActionEvent(PAUSE_COMMAND);
- playPauseButton1.setState(PlayPauseButton.PLAY_STATE);
- break;
- }
- }
- catch(NumberFormatException exc) { }
- }
-
- void closeBoxButton1_ActionPerformed(ActionEvent event)
- {
- setVisible(false);
- dispose();
- }
- }
-